Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
temp-write
Advanced tools
The temp-write npm package allows you to write temporary files to the filesystem. It is useful for scenarios where you need to create temporary files that are automatically cleaned up after use.
Write temporary file with content
This feature allows you to write a string to a temporary file and get the file path. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!');
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
Write temporary file with specific extension
This feature allows you to specify the extension of the temporary file. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!', { extension: 'txt' });
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
Write temporary file with specific name
This feature allows you to specify the name of the temporary file. The file is automatically cleaned up after the process exits.
const tempWrite = require('temp-write');
const fs = require('fs');
(async () => {
const filePath = await tempWrite('Hello, world!', { name: 'example.txt' });
console.log(filePath);
console.log(fs.readFileSync(filePath, 'utf8'));
})();
The tmp package provides similar functionality for creating temporary files and directories. It offers more control over the lifecycle of the temporary files and directories, including manual cleanup.
The temp package is another alternative for handling temporary files and directories. It provides a more comprehensive API for managing temporary resources, including automatic cleanup and custom directory support.
The fs-extra package extends the native fs module with additional methods, including methods for handling temporary files. It provides a more extensive set of file system utilities beyond just temporary file handling.
Write string/buffer to a random temp file
$ npm install --save temp-write
var fs = require('fs');
var tempWrite = require('temp-write');
var filepath = tempWrite.sync('unicorn');
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b
fs.readFileSync(filepath, 'utf8');
//=> unicorn
tempWrite.sync('unicorn', 'pony.png');
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/pony.png
tempWrite.sync('unicorn', 'rainbow/cake/pony.png');
//=> /var/folders/_1/tk89k8215ts0rg0kmb096nj80000gn/T/4049f192-43e7-43b2-98d9-094e6760861b/rainbow/cake/pony.png
Required
Type: string
, buffer
Type: string
Example: 'img.png'
, 'foo/bar/baz.png'
Optionally supply a filepath which is appended to the random path.
Required
Type: function
Type: string
, buffer
Returns: the filepath
MIT © Sindre Sorhus
FAQs
Write data to a random temporary file
The npm package temp-write receives a total of 458,732 weekly downloads. As such, temp-write popularity was classified as popular.
We found that temp-write demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.